from rpc_server.protobuf import Stocks_pb2 as StocksPb, Stocks_pb2_grpc as StocksGrpc
from stock import baoStock, East



class StocksServiceImpl(StocksGrpc.StocksServiceServicer):

    def LoadGeneStocks(self, request, context):
        stocks = East.load_gene_stocks(request.geneCode)
        records = stocks.to_dict(orient='records')
        try:
            records = [StocksPb.StockResp(**record) for record in records]
            print(records)
            return StocksPb.GeneStockResp(geneCode=request.geneCode, stocks=records)
        except Exception as e:
            print(e)

    def GetStockInfo(self, request, context):
        stock_info = baoStock.get_stock_info(request.code)
        return StocksPb.StockResp(code=stock_info['code'], name=stock_info['code_name'])

